import axios from 'axios'

/**
 * 封装请求的全路径
 * @param {Object} config
 */
const $globalRequestUrl = params => {
  const { VITE_BASE_URL } = import.meta.env
  const baseUrl = VITE_BASE_URL
  return `${params.baseUrl ? params.baseUrl : baseUrl}${params.url}`
}

/**
 * post请求
 * @param {Object} config
 */
const post = config => {
  return new Promise((resolve, reject) => {
    axios.post(config.url, config.params, { headers: config.header }).then(res => {
      resolve(res)
    }, error => {
      reject(error)
    })
  })
}

/**
 * PUT请求
 * @param {Object} config
 */
const put = config => {
  return new Promise((resolve, reject) => {
    axios.put(config.url, config.params, { headers: config.header }).then(res => {
      resolve(res)
    }, error => {
      reject(error)
    })
  })
}

/**
 * get请求
 * @param {Object} config
 */
const get = config => {
  return new Promise((resolve, reject) => {
    axios.get(config.url, {
      params: config.params,
      headers: config.header
    }).then(res => {
      resolve(res)
    }).catch(e => {
      reject(e)
    })
  })
}

/**
 * delete请求
 * @param {Object} config
 */
const deleteRequest = config => {
  return new Promise((resolve, reject) => {
    axios.delete(config.url, {
      data: config.params,
      headers: config.header
    }).then(res => {
      resolve(res)
    }, error => {
      reject(error)
    })
  })
}

/**
 * formData请求
 * @param {Object} config
 */
const formData = config => {
  return new Promise((resolve, reject) => {
    axios({
      url: config.url,
      method: 'post',
      data: config.params,
      headers: config.header
    }).then(res => {
      resolve(res)
    }, error => {
      reject(error)
    })
  })
}

/**
 * 全局请求方法
 * @param {String} url 请求地址
 * @param {*} params 请求参数
 * @param {String} resMethod 请求方法
 * @param {Object} resHeader 请求头
 * @returns
 */
const $globalRequest = (url = '', params = {}, resMethod = '', resHeader = null) => {
  try {
    // 请求方式规整，防止书写不一致
    const mStr = typeof resMethod === 'string' ? resMethod.toUpperCase() : resMethod

    const token = ''
    // 设置默认请求头和蚕食
    const header = {
      'Content-Type': 'application/json',
      Authorization: token,
      ...resHeader
    }

    // 请求数据封装
    const reqData = { url, header, params }

    if ([ 'POST' ].includes(mStr)) {
      return post(reqData).then(res => {
        return res
      })
    } else if ([ 'PUT' ].includes(mStr)) {
      return put(reqData).then(res => {
        return res
      })
    } else if ([ 'DELETE' ].includes(mStr)) {
      return deleteRequest(reqData).then(res => {
        return res
      })
    } else if ([ 'GET' ].includes(mStr)) {
      return get(reqData).then(res => {
        return res
      })
    } else if ([ 'FORMDATA' ].includes(mStr)) {
      return formData(reqData).then(res => {
        return res
      })
    }
    console.error('请求方式错误-----------------------请检查~@@~')
  } catch (error) {
    console.error('请求方式代码异常----------------------请检查-^^-', error)
  }
}

export { $globalRequestUrl, $globalRequest }
